/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.home.logic;

import com.home.common.utils.DateUtils;
import com.home.dao.ScorecardDao;
import com.home.dao.ScorecardDetailDao;
import com.home.dao.ScorecardDetailsResultDao;
import com.home.dao.ScorecardGroupAttributeResultDao;
import com.home.mapping.Scorecard;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 *
 * @author pnduy
 */
public class ScorecardLogic {
    
    public List<Scorecard> findScorecardsOfAgentInPeriod(List<Integer> listAgentIds, int week, Integer scorecardType) {
        List<Scorecard> list = new ArrayList<>();        
        Map<Integer, Date[]> mapWeekInYear = DateUtils.getMapWeekInCurrentYear();
        Date dateFrom = mapWeekInYear.get(week)[0];
        Date dateTo = mapWeekInYear.get(week)[1];
        list = new ScorecardDao().findScorecardsOfAgentsInPeriod(listAgentIds, dateFrom, dateTo, scorecardType);
        
        return list;
    }
    
    public Integer saveOrUpdateScorecard(Scorecard scorecard) {
        Integer scId = 0;
        try {
            
            if (scorecard.getIdScorecard() != null) {
                new ScorecardDao().updateScorecard(scorecard);
                new ScorecardDetailDao().deleteScorecardDetailsByScorecardId(scorecard.getIdScorecard());
                new ScorecardDetailsResultDao().deleteScorecardDetailsResultByScorecardId(scorecard.getIdScorecard());
                new ScorecardGroupAttributeResultDao().deleteByScorecardId(scorecard.getIdScorecard());
            } else {
                scorecard.setIdScorecard(new ScorecardDao().addNewScorecard(scorecard));
            }
            new ScorecardDetailDao().insertSetObject(scorecard.getScorecardDetailses());
            new ScorecardDetailsResultDao().insertSetObject(scorecard.getScorecardDetailsResults());
            new ScorecardGroupAttributeResultDao().insertSetObject(scorecard.getScorecardGroupAttributeResult());
            
        } catch (Exception e) {
            e.printStackTrace();
            scId = 0;
        }
        scId = scorecard.getIdScorecard();
        return scId;
    }
    
    public List<Scorecard> findScorecardsByAgent(Integer agentId) {
        return new ScorecardDao().findScorecardsByAgent(agentId);
    }
    
    public boolean deteteScorecard(Scorecard scorecard) {
        return new ScorecardDao().deteteScorecard(scorecard);
    }
            
    public Scorecard findScorecardByIdIncludeAgentSubject(Integer scorecardId) {
        Scorecard scorecard = new ScorecardDao().findScorecardByIdIncludeAgentSubject(scorecardId);
        

        return scorecard;
    }
    
    public List<Scorecard> findScForInboundVentureReport(int weekFrom, int weekTo, Integer scorecardTypeId) {
        List<Scorecard> list = new ArrayList<>();
        Map<Integer, Date[]> mapWeekInYear = DateUtils.getMapWeekInCurrentYear();
        Date dateFrom = mapWeekInYear.get(weekFrom)[0];
        Date dateTo = mapWeekInYear.get(weekTo)[1];
        
        list = new ScorecardDao().findScorecardsInPeriod(dateFrom, dateTo, scorecardTypeId);
               
        return list;
    }
    
}
